package cn.edu.hunau.oop.day09;

import org.junit.Test;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * @author hb
 * @date 2025-08-14 15:43:05
 */
public class TestByteStream {
    @Test
    public void write(){
        //将指定的字符串的内容写如到dir/a001.txt文件中
        String data="抗日胜利80周年";
        FileOutputStream fos=null;
        try {
            fos=new FileOutputStream("dir/a001.txt");
            fos.write(data.getBytes());
            fos.flush();//刷新数据

        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }finally{
            if(fos!=null){
                try {
                    //io资源为物理资源，必须手动关闭
                    fos.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }

    @Test
    public void write2(){
        //将指定的字符串的内容写如到dir/a001.txt文件中
        String data="2025年是抗日胜利80周年";
        try(FileOutputStream fos=new FileOutputStream("dir/b001.txt");){
            fos.write(data.getBytes());
            fos.flush();//刷新数据
        }catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
